package stu.sup.service.impl;

import com.github.pagehelper.PageHelper;
import org.apache.catalina.servlets.DefaultServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import stu.sup.dto.OrderDto;
import stu.sup.entity.*;
import stu.sup.mapper.OrdersMapper;

import stu.sup.service.*;

import stu.sup.service.IAddressInfoService;
import stu.sup.service.IOderService;
import stu.sup.service.IPicInfoService;
import stu.sup.service.IProductService;


import java.util.Date;
import java.util.List;

/**
 * Created by HUAWEI on 2020/6/23.
 */
@Service
@Transactional
public class OrderServiceImpl implements IOderService {
    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    IProductService productService;
    @Autowired
    IAddressInfoService adressInfoService;
    @Autowired
    IPicInfoService picInfoService;
    @Autowired
    IBuycarService buycarService;


    @Override
    public Orders saveorder(Integer userId ,Integer buycarId) {
        Orders orders=new Orders();
        Buycart buycart=buycarService.findbuid(buycarId);
        List<AddressInfo> addressInfos=adressInfoService.findbyuser(userId);
        orders.setUserLoginId(userId);
        orders.setProductId(buycart.getProductId());
        orders.setAddressId(addressInfos.get(0).getAddressInfoId());
        orders.setBuycartId(buycart.getBuycartId());
        orders.setOrderMoney(buycart.getProductPrice()*buycart.getProductNum());
        orders.setPaymentMethod("线上支付");
        orders.setCreateTime(new Date());
        int count=ordersMapper.insert(orders);
        return orders;
    }

    @Override
    public boolean delateorder(Orders orders) {
        int count=0;
        count=ordersMapper.deleteByPrimaryKey(orders.getOrderId());
        return count==0?false:true;
    }

    @Override
    public List<Orders> selectByuserId(UserLogin userLogin) {
        return ordersMapper.findAll(userLogin.getId());
    }

    @Override
    public int delatebyOrderId(Orders orders) {
        return ordersMapper.deleteByPrimaryKey(orders.getOrderId());
    }

    @Override
    public List<Orders> findAll(int pageNum, int PageSize, UserLogin userLogin) {
        PageHelper.startPage(pageNum, PageSize);
        return ordersMapper.findAll(userLogin.getId());
    }

    @Override
    public OrderDto creatOrder(Orders orders) {
        OrderDto orderDto=new OrderDto();
        Product product=productService.selectbyId(orders.getProductId());
        List<AddressInfo> addressInfo=adressInfoService.find(orders.getAddressId());
        ProductTpicInfo productTpicInfo=picInfoService.selectById(product.getProductImgId());
        orderDto.setOrderId(orders.getOrderId());
        orderDto.setProductName(product.getProductName());
        orderDto.setProductPrice(product.getProductPrice());
        orderDto.setProductDesc(product.getProductDesc());
        orderDto.setCreateTime(orders.getCreateTime());
        orderDto.setOrderStatus(orderDto.getOrderStatus());
        orderDto.setImgCount(productTpicInfo.getPicUrl());
        orderDto.setPaymentMethod(orders.getPaymentMethod());
        orderDto.setReciveTime(orders.getReciveTime());
        orderDto.setPaymentMoney(orders.getPaymentMoney());
        orderDto.setPaymentTime(orders.getPaymentTime());

        return orderDto;
    }


}
